/**
 * 绩效系数导入
 */
import React, { useEffect } from 'react';
import { connect } from 'umi';
import { withRoutePage } from '@/utils/enhanceUtils';
import ExcelList from '@/pages/SalaryManage/SalaryCalculate/ImportExcel/ExcelList';
import { Select } from 'antd';
import { formatMessage } from 'umi';

const UploadExtra = ({ dispatch, kpiSalaryItems, salaryItemId, organizationCode }) => {
    useEffect(() => {
        if (organizationCode) {
            dispatch({
                type: 'salaryBaseKpiModel/fetchKpiSalaryItems',
                payload: { organizationCode }
            });
        }
    }, [organizationCode]);

    const onSelect = selectedValue => {
        dispatch({
            type: 'salaryBaseKpiModel/setSalaryItemId',
            payload: selectedValue
        });
    };

    let selectValue = null;
    if (salaryItemId) {
        selectValue = salaryItemId;
    } else if (kpiSalaryItems && kpiSalaryItems.length) {
        selectValue = kpiSalaryItems[0].id;
    }

    useEffect(() => {
        dispatch({
            type: 'salaryBaseKpiModel/setSalaryItemId',
            payload: selectValue
        });
    }, [selectValue]);

    return (
        <div style={{ marginBottom: 30 }}>
            <span>
                {/* 绩效类型： */}
                {formatMessage({ id: 'salaryBaseKpi.upload.kpiType' })}：
            </span>
            <Select style={{ width: 200 }} value={selectValue} onSelect={onSelect}>
                {kpiSalaryItems.map(item => (
                    <Select.Option key={item.id} value={item.id}>
                        {item.itemName}
                    </Select.Option>
                ))}
            </Select>
        </div>
    );
};
const UploadExtraWrap = connect(({ salaryBaseKpiModel, importExcelModel }) => ({
    kpiSalaryItems: salaryBaseKpiModel.kpiSalaryItems,
    salaryItemId: salaryBaseKpiModel.salaryItemId,
    organizationCode: importExcelModel.selectOrganizationCode
}))(UploadExtra);

export default withRoutePage(
    connect(({ salaryBaseKpiModel, loading, user, importExcelModel }) => ({
        ...salaryBaseKpiModel,
        selectOrganizationCode: importExcelModel.selectOrganizationCode,
        organizationResponse: importExcelModel.organizationResponse,
        loading,
        dispatchType: {
            initType: 'salaryBaseKpiModel/init',
            changePeriodType: 'salaryBaseKpiModel/changePeriod',
            searchType: 'salaryBaseKpiModel/search',
            deleteType: 'salaryBaseKpiModel/deleteSalaryBaseKpi',
            tablePageChangeType: 'salaryBaseKpiModel/tablePageChange',
            showImportDialogType: 'salaryBaseKpiModel/showImportDialog',
            setMultiStoreType: 'salaryBaseKpiModel/setMultiStore',
            mergeSearchModelType: 'salaryBaseKpiModel/mergeSearchModel',
            downloadTemplateType: 'salaryBaseKpiModel/downloadTemplate'
        },
        showTemplateButton: salaryBaseKpiModel.kpiSalaryItems?.length > 0,
        templateUrl: '/api/finance/salary/v1/template/excels/kpiTemplate.xlsx',
        uploadAction: '/api/finance/salary/v1/import/kpi',
        user,
        uploadExtra: <UploadExtraWrap />,
        uploadExtraParams: () => `/${salaryBaseKpiModel.salaryItemId}/${importExcelModel.selectOrganizationCode}`,
        importAuthCode: 'finance:salary:computation:import:kpi:import'
    }))(ExcelList)
);
